package com.goldcontrol.cloudweb.quartz.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.goldcontrol.cloudweb.common.Constants;
import com.goldcontrol.cloudweb.util.ConstantUtil;
import com.goldcontrol.cloudweb.util.HttpUtil;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DynamicJob implements Job{

    private Logger logger = LoggerFactory.getLogger(DynamicJob.class);


    public void execute(JobExecutionContext jobExecutionContext){
        try{
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
            JobDetail jobDetail = jobExecutionContext.getJobDetail();
            Object val = jobDetail.getJobDataMap().get("val");
            Object devid = jobDetail.getJobDataMap().get("devid");
            Object itemid = jobDetail.getJobDataMap().get("itemid");
            Object state = jobDetail.getJobDataMap().get("state");

            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String now = sdf.format(date);
            if(val == null || devid == null || itemid == null || state == null){
                logger.error(now + " 参数不正确");
                return;
            }
            String token = Constants.TOKEN;

            if("1".equals(state.toString())){
                logger.info(now + " 执行了一次开机");
                String url = ConstantUtil.api_url+"control?"+"token="+token+"&hash=test&devid="+devid+"&itemid="+itemid+"&value="+val;
                String result = HttpUtil.put2(url);
                JSONObject jsonObject = JSON.parseObject(result);
                logger.info(result);
                if(!"100".equals(jsonObject.getString("status"))){
                    //重试3次
                    for(int i=0;i<3;i++){
                        String result1 = HttpUtil.put2(url);
                        logger.info("执行第"+(i+1)+"次重试，请求结果为:"+result1);
                    }
                }

            }else if("0".equals(state.toString())){
                logger.info(now + " 执行了一次关机");
                String url = ConstantUtil.api_url+"control?"+"token="+token+"&hash=test&devid="+devid+"&itemid="+itemid+"&value="+val;
                String result = HttpUtil.put2(url);
                JSONObject jsonObject = JSON.parseObject(result);
                logger.info(result);
                if(!"100".equals(jsonObject.getString("status"))){
                    //重试3次
                    for(int i=0;i<3;i++){
                        String result1 = HttpUtil.put2(url);
                        logger.info("执行第"+(i+1)+"次重试，请求结果为:"+result1);
                    }
                }

            }
        }catch (Exception e){
            logger.error("执行定时任务出错",e);
        }

    }

}